package com.project.service.impl;

import cn.hutool.json.JSONUtil;
import com.project.entity.Product;
import com.project.entity.StructuredTemplate;
import com.project.service.*;
import dev.langchain4j.data.document.Document;
import dev.langchain4j.data.document.DocumentSplitter;
import dev.langchain4j.data.document.DocumentTransformer;
import dev.langchain4j.data.document.loader.FileSystemDocumentLoader;
import dev.langchain4j.data.document.parser.TextDocumentParser;
import dev.langchain4j.data.document.splitter.DocumentByParagraphSplitter;
import dev.langchain4j.data.document.splitter.DocumentBySentenceSplitter;
import dev.langchain4j.data.embedding.Embedding;
import dev.langchain4j.data.segment.TextSegment;
import dev.langchain4j.model.embedding.EmbeddingModel;
import dev.langchain4j.store.embedding.EmbeddingSearchRequest;
import dev.langchain4j.store.embedding.EmbeddingSearchResult;
import dev.langchain4j.store.embedding.EmbeddingStore;
import dev.langchain4j.store.embedding.EmbeddingStoreIngestor;
import dev.langchain4j.store.embedding.inmemory.InMemoryEmbeddingStore;
import jakarta.annotation.PostConstruct;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import reactor.core.publisher.Flux;

import java.util.List;

/**
 * @Author: zhoujinchuan
 * @Description: （）
 * @Date: 2025/2/26 10:11
 */
@Slf4j
@Service
public class ChatServiceImpl implements IChatService {

    @Autowired
    private ProductManagerAssistant productManagerAssistant;

    @Autowired
    private CommonChatAssistant chatAssistant;

    @Autowired
    private ProductExtractor productExtractor;

    @Autowired
    private NetChatAssistant netChatAssistant;

    @Autowired
    private EmbeddingStore<TextSegment> embeddingStore;

    @Autowired
    private EmbeddingModel embeddingModel;


    @Override
    public Flux<String> chat(String msg,Boolean netStatus) {
        if (netStatus){
            return netChatAssistant.chatByNet(msg);
        }
        return chatAssistant.chat(msg);
    }

    @Override
    public Flux<String> productChat(Long userId, String msg) {
        return productManagerAssistant.chat(userId,msg);
    }

    @Override
    public Flux<String> chatFormStructuredPrompt(Long userId, String function) {
        StructuredTemplate template = new StructuredTemplate();
        template.setFunction(function);
        return productManagerAssistant.chatFormStructuredPrompt(userId,template);
    }

    @Override
    public Flux<String> extractChat(String msg) {
        Product product = productExtractor.extractProduct(msg);
        log.error("提取产品内容：{}",JSONUtil.toJsonStr(product));
        return chatAssistant.extractChat(msg);
    }

    /**
     * 初始化，将文档信息存到向量数据库中
     */
    @PostConstruct
    public void initDocxData() {

    }
}
